package com.zy.service.bs.impl;


import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.zy.dao.bs.ActivityDao;
import com.zy.dao.sys.UserRoleDao;
import com.zy.entity.bs.Activity;
import com.zy.entity.enums.StatusEnum;
import com.zy.entity.enums.bs.ActivityStatusEnum;
import com.zy.entity.sys.UserRole;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.pojo.query.bs.ActivityQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.ActivityService;
import com.zy.service.fileutil.FileUploadService;
import com.zy.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Service("activityService")
public class ActivityServiceImpl extends BaseServiceImpl<Activity> implements ActivityService {

	@Autowired
	private ActivityDao activityDao;
    @Autowired
    private FileUploadService fileUploadService;
    @Value("${fileUploadPath}")
    private String fileUploadPath;
    @Autowired
    private UserRoleDao userRoleDao;

	public PageVo pageActivity(ActivityQuery activityQuery, Integer start, Integer limit) {
		List<Activity> Activitys = this.activityDao.listActivity(activityQuery, start, limit);
//        Activitys.forEach((Activity activity) ->{
//            if(activity.getHomeImage()!=null){
//                activity.setHomeImagePath(fileUploadPath+activity.getHomeImage());
//            }
//        });
		return new PageVo(start, limit, this.activityDao.countActivity(activityQuery), Activitys);
	}

	@Override
	public void saveActivity(Activity activity) {
		long time = DateUtil.getTime();
        if(activity.getHomeImage().isEmpty()){
            throw new ServiceException("活动首页图片不能为空！");
        }
        if(activity.getEnrollBeginTime().compareTo(activity.getBeginTime()) > 0){
			throw new ServiceException("报名开始时间大于活动开始时间");
		}else if(activity.getEnrollEndTime().compareTo(activity.getEndTime()) > 0){
			throw new ServiceException("报名结束时间大于活动结束时间");
		}
        Example ex = new Example(Activity.class);
        ex.createCriteria().andEqualTo("userId",activity.getUserId()).andIsNotNull("clubId");
        UserRole userRole = userRoleDao.getByExample(ex);
        activity.setClubId(userRole.getClubId());
		activity.setCreateTime(time);
		activity.setModifyTime(time);
		activity.setStatus(StatusEnum.NORMAL.getValue());
		this.activityDao.save(activity);
	}
	
	@Override
	public void updateActivity(Activity activity) {

			activity.setModifyTime(DateUtil.getTime());
			this.activityDao.updateByPrimaryKeySelective(activity);
	}

	@Override
	public void removeActivity(long id) {
		try {
			this.activityDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			Activity activity = new Activity();
			activity.setId(id);
			activity.setModifyTime(DateUtil.getTime());
			activity.setStatus(StatusEnum.DELETE.getValue());
			this.activityDao.updateByPrimaryKeySelective(activity);
		}
	}

	@Override
	public void recover(long id) {
		Activity activity = new Activity();
		activity.setId(id);
		activity.setModifyTime(DateUtil.getTime());
		activity.setStatus(StatusEnum.NORMAL.getValue());
		this.activityDao.updateByPrimaryKeySelective(activity);
	}

	@Override
	public List<Activity> listActivityForHomePage() {
		return this.activityDao.listActivityForHomePage(DateUtil.getTime());
	}

	@Override
	public PageVo pageFrontActivity(ActivityQuery activityQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.activityDao.countFrontActivity(activityQuery), activityDao.listFrontActivity(activityQuery,start,limit));
	}

	@Override
	public List<Activity> listFourActivity() {
		return activityDao.listFourActivity();
	}

	@Override
	public void updateActivityStatus(Activity activity) {
		Activity activity1 = this.activityDao.getByPrimaryKey(activity.getId());
		if(!Objects.isNull(activity1)){
			if(activity1.getActivityStatus() == ActivityStatusEnum.PUBLISHED.getValue()){
				activity.setActivityStatus(ActivityStatusEnum.ALREADY_DOWN.getValue());
			}
			activity.setModifyTime(DateUtil.getTime());
			this.activityDao.updateByPrimaryKeySelective(activity);
		}
	}

    @Override
    public PageVo myActivity(Long userId, Integer start, Integer limit) {
        return new PageVo(start, limit, this.activityDao.myActivityCount(userId), activityDao.myActivityList(userId,start,limit));
    }

    @Override
    public PageVo myManagerActivityList(Long userId, Integer start, Integer limit) {
        return new PageVo(start, limit, this.activityDao.countManagerActivity(userId), activityDao.myManagerActivityList(userId,start,limit));
    }

}
